Changing a model's database connection

Skip to first unread message

Michaël

unread,
Jul 19, 2010, 1:08:36 PM7/19/10
to Ruby on Rails: Talk
Hi,

I've got a simple problem. I've got a collection of models, Customer,
Product and Shop, and they're all using the database configured for
the ActiveRecord class. However, for one particular task -- generating
a report -- I want to be able to set those models to use a database on
a different server. The secondary database is simply a copy of the
first, so the schemas and data is identical to the main database, but
I want to use this secondary server so that the load of producing
reports doesn't affect the main database server.

I've tried the following, but it doesn't seem to have any effect:

def build_report:

Customer.establish_connection(
:adapter => "mysql",
:host => "hostname",
:username => "user",
:password => "password",
:database => "schema"
)

@customer = Customer.find(@criteria[:customer_id])

..... <report logic here>

end

Is it possible to achieve what I want to do this way, or do I need to
look along other lines?

Many thanks,
Michaël

Marnen Laibow-Koser

unread,
Jul 19, 2010, 2:14:55 PM7/19/10
to rubyonra...@googlegroups.com
Michaël wrote:
> Hi,
>
> I've got a simple problem. I've got a collection of models, Customer,
> Product and Shop, and they're all using the database configured for
> the ActiveRecord class. However, for one particular task -- generating
> a report -- I want to be able to set those models to use a database on
> a different server. The secondary database is simply a copy of the
> first, so the schemas and data is identical to the main database, but
> I want to use this secondary server so that the load of producing
> reports doesn't affect the main database server.

I think you would be better advised to use your DB server's
clustering/load-balancing features for this. It should not be your
application's concern.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Jul 19, 2010, 2:32:33 PM7/19/10
to Ruby on Rails: Talk


On Jul 19, 6:08 pm, Michaël <michael.b...@gmail.com> wrote:
> Hi,
> I've tried the following, but it doesn't seem to have any effect:
>
>      def build_report:
>
>         Customer.establish_connection(
>             :adapter  => "mysql",
>             :host     => "hostname",
>             :username => "user",
>             :password => "password",
>             :database => "schema"
>         )
>
>         @customer = Customer.find(@criteria[:customer_id])
>
>         ..... <report logic here>
>
>      end
>
> Is it possible to achieve what I want to do this way, or do I need to
> look along other lines?
>

this would only affect queries done against the customers table.
queries to any other tables would still go to the 'normal' database.
Is that what's happening?

Fred
> Many thanks,
> Michaël

Michaël

unread,
Jul 19, 2010, 2:42:07 PM7/19/10
to Ruby on Rails: Talk


On 19 July, 19:32, Frederick Cheung <frederick.che...@gmail.com>
wrote:
That's actually what I want to achieve, but I've done the above and it
doesn't seem to work. I know that reports only use information from
the Customers, Products and Shop models, so I only want to change the
database connection for these three while generating the report, and
then switch it back after its done.

As far as load balancing is concerned, my ultimate goal is to push
that to the database level and have the database server handle that,
but I need to implement something quickly as an intermediate step.

Marnen Laibow-Koser

unread,
Jul 19, 2010, 2:45:46 PM7/19/10
to rubyonra...@googlegroups.com
Michaël wrote:
[...]

> As far as load balancing is concerned, my ultimate goal is to push
> that to the database level and have the database server handle that,
> but I need to implement something quickly as an intermediate step.

Then do it on the DB side now. It probably won't take any more time
than all the research you're already doing to do this on the app side,
and you won't have to throw the work away later.

Bob Proulx

unread,
Jul 19, 2010, 3:30:10 PM7/19/10
to rubyonra...@googlegroups.com
Micha�l wrote:
> I want to be able to set those models to use a database on a
> different server.
> ...

> I've tried the following, but it doesn't seem to have any effect:

I do this for switching between a legacy db and a new db. It works
fine. But I was doing it a slightly different way and it.

I think the simplest way is to define a new database in your
config/database.yml file. You should be able to pass in all of the
parameters inline too but that tends not to be DRY as you will almost
certainly need to refer to it more than once. So having it in one
place in the database.yml is nice and only once. Something like this:

myotherdb:
adapter: mysql
username: myusername
password: mypassword
database: myotherdatabase

Then to always use it for a model then refer to it by name using
establish_connection. Like this:

class User < ActiveRecord::Base
establish_connection "myotherdb"
...
end

Since you only want to change to it sometimes dynamically depending
upon what you are doing then you can call establish_connection inline
and switch over from one db to the other. Since you are manually
explicitly changing db's out from under activerecord then don't forget
to switch back to the standard one afterward. Not doing that was a
source of bugs in my own projects. The symptom was that sometimes,
depending upon the program flow, it would end up talking to the wrong
database.

def build_report
ActiveRecord::Base.establish_connection("myotherdb") if ! ENV['RAILS_ENV'] == 'test'
... do your other db stuff here ...
ActiveRecord::Base.establish_connection(ENV["RAILS_ENV"]) if ! ENV['RAILS_ENV'] == 'test'
end

Note the use of ENV["RAILS_ENV"] to select the right database when
returning. That assumes that you are using the standard "production"
and "development" names. Note the avoidance of this switch when
running tests. Two gotchas that I tripped over when first setting
this up.

Also, an unrelated side remark, ruby isn't python and you don't need
to end lines with colons, as you had done in your example. :-)

Bob

P.S. Sorry if the thread of this message is broken. I had a personal
snafu and needed to recreate the message from parts.

Michaël

unread,
Jul 19, 2010, 5:21:09 PM7/19/10
to Ruby on Rails: Talk
Thanks for the clarification. This is exactly what I've done, and it's
working now. I was struggling because there was a rogue backgroundrb
task running for reports generation that still had the primary server
configured for its database connection.
>
> Also, an unrelated side remark, ruby isn't python and you don't need
> to end lines with colons, as you had done in your example.  :-)
>

I just noticed that :) It's not in my code fortunately. That's what
comes of trying to manage too many projects in different languages.

Thanks for all the help, guys.
Reply all
Reply to author
Forward
0 new messages